<?php
/**
 *	作者：耿鸿飞
 *	日期：2011年10月29日
 *	作用：数据库操作类
 **/
class G_db{
	private $_con = null;//数据库连接
	private $_host = null;//数据库db地址
	private $_dbname = null;//数据库名字
	private $_user = null;//数据库用户名
	private $_pass = null;//数据库密码
	private $_charset = null;//数据库中的字符编码
	private $_pcon = false;//数据是否要长连接
	private $_results = false;
	function G_db($host,$dbname,$user,$pass,$charset,$pcon = false){
		$this->_host = $host;
		$this->_dbname = $dbname;
		$this->_user = $user;
		$this->_pass = $pass;
		$this->_charset = $charset;
		$this->_pcon = $pcon;
		$this->connect();
	}
	//创建数据库的连接
	private function connect(){		
		if($this->_pcon){
			$this->_con = mysql_pconnect($this->_host,$this->_user,$this->_pass); 
		}else{
			$this->_con = mysql_connect($this->_host,$this->_user,$this->_pass);
		}
		if($this->_con){
			if(mysql_select_db($this->_dbname,$this->_con)){
				if(mysql_set_charset($this->_charset,$this->_con)){
				}else{
					echo "设置字符编码错误";die();
					//设置字符编码失败
				}
			}else{//echo mysql_error();
				echo "选择数据库失败";die();
				//选择数据库
			}
		}else{
			
			echo "连接数据库失败";die();
			//连接错误了
		}
	}
	/**
	 *	查询数据
	 **/
	public function query($sql){
		if(!mysql_ping($this->_con)){
			$this->connect();
		}//echo $sql;
		$this->_results = mysql_query($sql,$this->_con);
		return $this->_results;
	}
	public function get_var($sql){
		if($this->query($sql)){
			$row = mysql_fetch_array($this->_results,MYSQL_NUM);
			mysql_free_result($this->_results);
			if($row){
				return $row[0];
			}
		}
		return null;
	}
	public function get_row($sql){
		if($this->query($sql)){
			$row = mysql_fetch_array($this->_results,MYSQL_ASSOC);
			mysql_free_result($this->_results);
			return $row;
		}
		return null;
	}
	public function get_results($sql){
		if($this->query($sql)){
			$rs = array();
			while($row = mysql_fetch_array($this->_results,MYSQL_ASSOC)){
				$rs[] = $row;
			}
			mysql_free_result($this->_results);
			return $rs;
		}
		return null;
	}
	public function insertTable($table,$set){
		if(is_array($set) AND COUNT($set) > 0){			
			if(count($sets) > 0){
				$SQL = sprintf("INSERT %s(%s) VALUES('%s')",$table,implode(",",array_keys($set)),implode("','",array_values($set)));
				if($this->query($SQL)){
					return $this->get_insertID();
				}
			}
		}
		return 0;
	}
	public function updateTable($table,$set,$where = null){
		if(is_array($set)){
			$sets = array();
			foreach($set AS $k=>$k){
				$sets[] = sprintf("%s='%s'",$k,$v);
			}
			if(count($sets) > 0){
				$SQL = sprintf("UPDATE %s SET %s %s",$table,implode(',',$sets),($where ? "WHERE $where" : ""));
				if($this->query($SQL)){
					return $this->get_rows();
				}
			}
		}
		return 0;
	}
	public function del_row($table,$where = null){
		$SQL = sprintf("DELETE FROM %s %s",$table,($where ? "WHERE $where" : ""));
		if($this->query($SQL)){
			return $this->get_rows();
		}
		return 0;
	}
	public function get_insertID(){
		return mysql_affected_rows($this->_con);
	}
	public function get_rows(){
		return mysql_affected_rows($this->_con);
	}
}